using Microsoft.VisualStudio.TestTools.UnitTesting;
using kratos;
using rpc;
using System.Collections.Generic;

namespace test_standard_rpc {
[TestClass]
public class UnitTest1 {
  ulong login_id = 0;
  string ret_string;
  Dictionary<long, Dummy> ret_dict;

  void Update(IRpc rpc, long timeout = 500) {
    var start = Util.GetNowMS();
    while (true) {
      rpc.Update();
      if (Util.GetNowMS() - start > timeout) {
        break;
      }
    }
    rpc.Close();
  }

  async void DoCall1(IRpc rpc) {
    bool ret = await rpc.Connect("127.0.0.1", 7288, 2000);
    var prx = Proxy.GetLogin();
    for (int i = 0; i < 10; i++) {
      login_id = await prx.login("123", "456", 2000);
    }
  }

  [TestMethod]
  public void TestCall1() {
    var rpc = RpcFactory.create();
    DoCall1(rpc);
    Update(rpc, 3000);
    rpc.Close();
    Assert.IsTrue(login_id != 0);
  }

  async void DoCall2(IRpc rpc) {
    bool ret = await rpc.Connect("127.0.0.1", 7288, 2000);
    var prx = Proxy.GetServiceDynamic();
    for (int i = 0; i < 10; i++) {
      HashSet<string> set_string = new HashSet<string>();
      set_string.Add("a");
      set_string.Add("b");
      ret_string = await prx.method2(1, set_string, 100, 1000);
    }
  }

  [TestMethod]
  public void TestCall2() {
    var rpc = RpcFactory.create();
    DoCall2(rpc);
    Update(rpc, 2000);
    rpc.Close();
    Assert.IsTrue(ret_string == "hello world");
  }

  async void DoCall3(IRpc rpc) {
    bool ret = await rpc.Connect("127.0.0.1", 7288, 2000);
    var prx = Proxy.GetServiceDynamic();
    var prx_my = Proxy.GetMyService();
    for (int i = 0; i < 10; i++) {
      HashSet<string> set_string = new HashSet<string>();
      set_string.Add("a");
      set_string.Add("b");
      Dictionary<long, string> dict = new Dictionary<long, string>();
      dict.Add(1, "a");
      dict.Add(2, "b");
      ret_dict = await prx.method3(1, set_string, dict, 100, 1000);      
    }
    var ret1 = await prx_my.method1();
    var ret2 = await prx_my.method2();
    var ret3 = await prx_my.method3();
    var ret4 = await prx_my.method4();
    var ret5 = await prx_my.method5();
    var ret6 = await prx_my.method6();
    var ret7 = await prx_my.method7();
    var ret8 = await prx_my.method8();
    var ret9 = await prx_my.method9();
    var ret10 = await prx_my.method10();
    var ret11 = await prx_my.method11();
    var ret12 = await prx_my.method12();
    var ret13 = await prx_my.method13();
    var ret14 = await prx_my.method14();
    var ret15 = await prx_my.method15();
    var ret16 = await prx_my.method16();
    var ret17 = await prx_my.method17();
    var list_data = new List<Data>();
    list_data.Add(Data.Create());
    var ret18 = await prx_my.method18(1, 2, 3, 4, 5, 6, 7, 8, false, "str", 1.0f, 2.0, Data.Create(), list_data, new List<string>(), new Dictionary<uint, Data>(), new HashSet<string>());
  }

  [TestMethod]
  public void TestCall3() {
    var rpc = RpcFactory.create();
    DoCall3(rpc);
    Update(rpc, 2000);
    rpc.Close();
    Assert.IsTrue(ret_dict.Count == 1 && (ret_dict[1] != null));
  }

  async void DoConnect(IRpc rpc) {
    await rpc.Connect("127.0.0.1", 33333, 2000);
  }

  [TestMethod]
  public void TestAutoReconnect1() {
    var rpc = RpcFactory.create();
    rpc.AutoReconnect = true;
    rpc.AutoReconnectIntvalMS = 1000;
    DoConnect(rpc);
    Update(rpc, 5000);
  }
}
}
